package com.lagou.test;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class IpersistenceTest02 {
    @Test
    public void test() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();

        User user = new User();
        user.setId(1);
        user.setUsername("admin");
        User user2 = sqlSession.selectOne("user.selectOne", user);
        System.out.println(user2);
    }

    @Test
    public void test2() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();

//        User user = new User();
//        user.setId(3);
//        user.setUsername("aaa");
//        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
//        userDao.addUser(user);

//        User user = new User();
//        user.setId(3);
//        user.setUsername("bbb");
//        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
//        userDao.updateUser(user);

//        User user = new User();
//        user.setId(3);
//        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
//        userDao.deleteUser(user);

        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        List<User> users = userDao.findAll();
        for (User user:users){
            System.out.println(user);
        }

//        User user = new User();
//        user.setId(3);
//        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
//        User byCondition = userDao.findByCondition(user);
//        System.out.println(byCondition);
    }
}
